<?php
class CI_Upload {
	// thu muc upload goc
	private $_root_upload_dir;
	// thu muc upload theo Nam/Thang/Ngay
	private $_current_upload_dir;
	// duong dan upload 
	private $_upload_path;

	// loai file cho phep
	private $_allow_type = array('video/mp4','audio/mp4','audio/ogg', 'video/ogg','audio/webm', 'video/webm','audio/mpeg','video/3gpp ','audio/wave', 'audio/wav', 'audio/x-wav'  );
	// dung luong cho phep
	private $_allow_size = SIZE_VIDEO ;//100M
	// chua thong tin loi
	private $_error_message = '';
	public function __construct($folder) {
		// kiem tra thu muc ton tai
		$this->_root_upload_dir    = $folder;
		//$this->_current_upload_dir = date('Y/m/d/');	
        $folderUpload              = $this->_root_upload_dir ;    //.'/'. $this->_current_upload_dir    	
		if( !file_exists ($folderUpload) ) {
			// tao thu muc				
			self::mkDirectory($folderUpload);
		}
		$this->_upload_path = $folderUpload; 
	}	
	public function do_upload( $oFile ){	
            //Kiem tra form submit
    		if ( ! isset($oFile) ) {
                $this->_error_message = 'No file upload.';
    			return null;
    		}
    		// neu co loi	
    		if( $oFile['error'] > 0 ){
    			$this->_error_message = 'file error';
    			return null;
    		}	
    		// kiem tra kieu file
    		if( ! in_array($oFile['type'], $this->_allow_type ) ) {
                $this->_error_message = 'File type disallow';
    			return null;
    		}	
    		// kiem tra dung luong
    		if( $oFile['size'] > $this->_allow_size ) {
                $this->_error_message = 'Max file size allow';
    			return null;
    		}
			// thuc hien upload
    		if(is_uploaded_file($oFile['tmp_name']))
			{
				$filename = stripslashes($oFile['name']);
				$path_info = pathinfo($filename); 
				$extension = strtolower($path_info['extension']) ;
				$name= md5($oFile['name']).'_'.date("dmYHis").'.'.$extension;
				$filename = $this->_root_upload_dir.'/'.$name ;
				if(move_uploaded_file($oFile['tmp_name'],$filename)){
					return $name;
				}else{
					return false;  
				}              
			}else{
				return false; 
			}
	}
	public static function mkDirectory($strTarget) {
		// from php.net/mkdir user contributed notes
		if (file_exists ( $strTarget )) {
			if (! @ is_dir ( $strTarget ))
				return false;
			else
				return true;
		}
		
		// Attempting to create the directory may clutter up our display.
		if (@ mkdir ( $strTarget )) {
			$stat = @ stat ( dirname ( $strTarget ) );
			$dir_perms = $stat ['mode'] & 0007777; // Get the permission bits.
			@ chmod ( $strTarget, $dir_perms );
			return true;
		} else {
			if (is_dir ( dirname ( $strTarget ) ))
				return false;
		}
		
		// If the above failed, attempt to create the parent node, then try again.
		if (self::mkDirectory ( dirname ( $strTarget ) ))
			return mkDir ( $strTarget );
		
		return false;
	}
	/**
	** options: exact, portrait, landscape, auto, crop 
	**
	**/
	public function resize($source,$save,$option='auto',$width=100,$height=100){
		/*include 'thumbnail.php';
		$thumb=new CI_Thumbnail($source);	
		switch($option){
			case 'width':
				$thumb->size_width($width);
				break;
			case 'height':
				$thumb->size_height($height);
				break;
			case 'auto':
				$thumb->size_auto($width);
				break;
			case 'size':
				$thumb->size($width,$height);
				break;				
		}
		$thumb->process();
		$thumb->show();	
		$thumb->save($save);*/
		include 'resize.php';
		// create folder
		if( !file_exists ($this->_root_upload_dir.'/small') ) {
			// tao thu muc				
			self::mkDirectory($this->_root_upload_dir.'/small');
		}
		$resizeObj=new Resize($source);
		$resizeObj -> resizeImage($width, $height, $option);
		$resizeObj -> saveImage($save, 100);
	}
	
	public function getName($source){
		$arr=pathinfo($source);
		return $arr['filename'].'.'.$arr['extension'];
	}

	public function getDirImage($dir){
		$arrdir=explode('/',$dir);
		$arrdir= end($arrdir);
		return $arrdir;
	}
}
/* ****************************** End of upload class ************************** */